package com.zzt.zt_lifecycle;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Lifecycle;

import com.zzt.zt_lifecycle.fragment.FullscreenFragment;

/**
 * @author: zeting
 * @date: 2022/12/19
 */
public class StartAct extends AppCompatActivity {
    public static void start(Context context) {
        Intent starter = new Intent(context, StartAct.class);
        context.startActivity(starter);
    }

    private FrameLayout fl_content;
    private FullscreenFragment cardFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        initView();
    }

    private void initView() {
        fl_content = findViewById(R.id.fl_content);


        cardFragment = FullscreenFragment.newInstance();

        Button frag1 = findViewById(R.id.btn_frag1);
        frag1.setText("CREATED");
        frag1.setOnClickListener(v ->
                setFragmentV1(cardFragment)
        );
        Button frag2 = findViewById(R.id.btn_frag2);
        frag2.setText("STARTED");
        frag2.setOnClickListener(v ->
                setFragmentV2(cardFragment)
        );
        Button frag3 = findViewById(R.id.btn_frag3);
        frag3.setText("RESUMED");
        frag3.setOnClickListener(v ->
                setFragmentV3(cardFragment)
        );

        Button frag4 = findViewById(R.id.btn_frag4);
        frag4.setText("再次 CREATED");
        frag4.setOnClickListener(v ->
                setFragmentV4(cardFragment, Lifecycle.State.CREATED)
        );

        Button frag5 = findViewById(R.id.btn_frag5);
        frag5.setText("再次 STARTED");
        frag5.setOnClickListener(v ->
                setFragmentV4(cardFragment, Lifecycle.State.STARTED)
        );

        Button btn_frag6 = findViewById(R.id.btn_frag6);
        btn_frag6.setText("再次 RESUMED");
        btn_frag6.setOnClickListener(v ->
                setFragmentV4(cardFragment, Lifecycle.State.RESUMED)
        );

    }

    public void setFragmentV1(FullscreenFragment cardFragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.fl_content, cardFragment);
        fragmentTransaction.setMaxLifecycle(cardFragment, Lifecycle.State.CREATED);
        fragmentTransaction.commit();
    }

    public void setFragmentV2(FullscreenFragment cardFragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.fl_content, cardFragment);
        fragmentTransaction.setMaxLifecycle(cardFragment, Lifecycle.State.STARTED);
        fragmentTransaction.commit();
    }

    public void setFragmentV3(FullscreenFragment cardFragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.fl_content, cardFragment);
        fragmentTransaction.setMaxLifecycle(cardFragment, Lifecycle.State.RESUMED);
        fragmentTransaction.commit();
    }

    public void setFragmentV4(FullscreenFragment cardFragment, Lifecycle.State state) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.setMaxLifecycle(cardFragment, state);
        fragmentTransaction.commit();

    }

}
